package com.zprmb.udzt.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zprmb.udzt.BbtApplication;
import com.zprmb.udzt.R;
import com.zprmb.udzt.bean.ExpandableBean;
import com.zprmb.udzt.bean.Product_Uplan;
import com.zprmb.udzt.util.BitmapUtils;
import com.zprmb.udzt.util.ExplosionField;
import com.zprmb.udzt.util.Regular;

import java.text.DecimalFormat;
import java.util.List;

import static com.zprmb.udzt.R.id.text_zhekou;

public class ExpandableBaseAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<ExpandableBean> list;
    DecimalFormat df = new DecimalFormat("###0.00");
    DecimalFormat df2 = new DecimalFormat("###0.0");
    private ExplosionField mExplosionField;//粉碎动画执行方法类
    private boolean myflag = false;

    //  if (myflag==true){
//        h.img_pl_reward.setVisibility(View.VISIBLE);
//        h.img_pl_reward.setImageResource(R.drawable.chongyanng);
//    }else {
//        h.img_pl_reward.setVisibility(View.GONE);
//    }
    public ExpandableBaseAdapter(Context context, List<ExpandableBean> list) {
        super();
        mExplosionField = ExplosionField.attach2Window((Activity) context);
        this.context = context;
        this.list = list;
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub

        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub

        return list.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return list.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Help h = null;
        if (convertView == null) {
            h = new Help();
            convertView = LayoutInflater.from(context).inflate(R.layout.group_demo, null);
            h.li_newuser = (LinearLayout) convertView.findViewById(R.id.li_newuser);
            h.tv_title = (TextView) convertView.findViewById(R.id.tv_product_name);
            h.tv_red = (TextView) convertView.findViewById(R.id.tv_new_user);
            h.tv_fresh = (TextView) convertView.findViewById(R.id.tv_fresh);
            h.tv_coupon = (TextView) convertView.findViewById(R.id.conduct_addrate);
            h.tv_minamount = (TextView) convertView.findViewById(R.id.tv_minamount);
            h.text_zhekou = (TextView) convertView.findViewById(text_zhekou);
            convertView.setTag(h);
        } else {
            h = (Help) convertView.getTag();
        }
        if (list.get(groupPosition).isFlagRebate() == true) {
            h.text_zhekou.setVisibility(View.VISIBLE);
        } else {
            h.text_zhekou.setVisibility(View.VISIBLE);
        }


        if ("FIRST_INVEST".equals(list.get(groupPosition).getProduct())) {

            h.li_newuser.setVisibility(View.GONE);
            h.tv_fresh.setVisibility(View.VISIBLE);
        } else {

            h.tv_fresh.setVisibility(View.GONE);
            h.li_newuser.setVisibility(View.VISIBLE);
        }
        h.tv_title.setText(list.get(groupPosition).getProductName());
        if (list.get(groupPosition).isFlagRaiseInterest()) {
            h.tv_coupon.setVisibility(View.VISIBLE);
//            h.text_zhekou.setVisibility(View.VISIBLE);
        } else {
            h.tv_coupon.setVisibility(View.VISIBLE);
            h.tv_coupon.setVisibility(View.VISIBLE);
        }
        if (list.get(groupPosition).isFlagRedpack()) {
            h.tv_red.setVisibility(View.VISIBLE);
        } else {
            h.tv_red.setVisibility(View.VISIBLE);
        }
        if (list.get(groupPosition).getMinAmount() != null) {

            h.tv_minamount.setText(list.get(groupPosition).getPrompt());
        }

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHelp h;

//		 slideView = (ZQView) convertView;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.v6_ud_item, null);
//			
//			slideView = new ZQView(context);
//			slideView.setContentView(itemView);
            h = new ViewHelp();
            h.v6_ud_invest_increaseNum = (TextView) convertView.findViewById(R.id.v6_ud_invest_increaseNum);
            h.tv_name = (TextView) convertView.findViewById(R.id.conduct_name);
            h.tv_time = (TextView) convertView.findViewById(R.id.conduct_time);
            h.tv_rate = (TextView) convertView.findViewById(R.id.conduct_rate);
            h.tv_rateadd = (TextView) convertView.findViewById(R.id.home_rateadd);
            h.tv_investAmount = (TextView) convertView.findViewById(R.id.conduct_investAmount);
            h.li_jiaxi = (LinearLayout) convertView.findViewById(R.id.li_jiaxi);
            h.img_list_status = (ImageView) convertView.findViewById(R.id.img_list_status);
            h.img_pl_reward = (ImageView) convertView.findViewById(R.id.img_pl_reward);
            h.tv_pl_reward = (TextView) convertView.findViewById(R.id.tv_pl_reward);
            h.sk_progresss = (ProgressBar) convertView.findViewById(R.id.sk_progresss);

            convertView.setTag(h);
        } else {
            h = (ViewHelp) convertView.getTag();
        }

        myflag = list.get(groupPosition).getList().get(childPosition).isFlagRestival();


        Product_Uplan uplan = list.get(groupPosition).getList().get(childPosition);
        if (uplan.getStatus().equals("COLLECTING")) {
            h.img_list_status.setVisibility(View.GONE);
        } else if (uplan.getStatus().equals("SETTLEMENTED")) {
            h.img_list_status.setVisibility(View.VISIBLE);
            h.img_list_status.setImageBitmap(BitmapUtils.readBitMap(context, R.drawable.img_list_shouyiz));
        } else if (uplan.getStatus().equals("REPAYED")) {
            h.img_list_status.setVisibility(View.VISIBLE);
            h.img_list_status.setImageBitmap(BitmapUtils.readBitMap(context, R.drawable.img_list_back_money));
        } else {
            h.img_list_status.setVisibility(View.VISIBLE);
            h.img_list_status.setImageBitmap(BitmapUtils.readBitMap(context, R.drawable.img_list_finsh));
        }
        h.v6_ud_invest_increaseNum.setText(list.get(groupPosition).getList().get(childPosition).getMinAmount() + "元起投");
        h.tv_name.setText(list.get(groupPosition).getList().get(childPosition).getProductName());
//		}
        h.sk_progresss.setProgress((int) (uplan.getDegree() * 10));
        h.tv_rate.setText(df.format(Double.parseDouble(list.get(groupPosition).getList().get(childPosition).getFixedRate())));
        if (!uplan.getFlagNowEffective()) {
            h.tv_pl_reward.setVisibility(View.GONE);
            if ("nonArrivalOpenTime".equals(uplan.getNonArrivalOpenTimeStatus())) {
                h.tv_pl_reward.setVisibility(View.VISIBLE);
                h.tv_pl_reward.setText(Regular.getMilliToHour_Min(uplan.getOpenTime()) + "开放");
                h.img_pl_reward.setVisibility(View.GONE);
            } else {
                if ("true".equals(uplan.getFlagFresh())) {
                    BbtApplication.getInstance().display(uplan.getFreshPictureAddress(), h.img_pl_reward);
                    h.img_pl_reward.setVisibility(View.VISIBLE);
                } else {


                    if (myflag == true) {
                        h.img_pl_reward.setVisibility(View.VISIBLE);
//                        h.img_pl_reward.setImageResource(R.drawable.chongyanng);
                        BbtApplication.getInstance().display(list.get(groupPosition).getList().get(childPosition).getFlagRestivalAddress(), h.img_pl_reward);
                    } else {
                        h.img_pl_reward.setVisibility(View.GONE);
                    }
//                    h.img_pl_reward.setVisibility(View.GONE);
                }
            }
        } else {
            if ("nonArrivalOpenTime".equals(uplan.getNonArrivalOpenTimeStatus())) {
                h.tv_pl_reward.setVisibility(View.VISIBLE);
                h.tv_pl_reward.setText(Regular.getMilliToHour_Min(uplan.getOpenTime()) + "开放");
//                h.img_pl_reward.setVisibility(View.GONE);

                if (myflag == true) {
                    h.img_pl_reward.setVisibility(View.VISIBLE);
//                    h.img_pl_reward.setImageResource(R.drawable.chongyanng);
//                    BbtApplication.getInstance().display(uplan.getFreshPictureAddress(), h.img_pl_reward);
                    BbtApplication.getInstance().display(list.get(groupPosition).getList().get(childPosition).getFlagRestivalAddress(), h.img_pl_reward);
                } else {
                    h.img_pl_reward.setVisibility(View.GONE);
                }
            } else {
                if ("true".equals(uplan.getFlagFresh())) {
                    h.tv_pl_reward.setVisibility(View.GONE);
                    h.img_pl_reward.setVisibility(View.VISIBLE);
                    BbtApplication.getInstance().display(uplan.getFreshPictureAddress(), h.img_pl_reward);
                } else {
                    h.tv_pl_reward.setVisibility(View.VISIBLE);
                    h.tv_pl_reward.setText("平台奖励");
//                    h.img_pl_reward.setVisibility(View.GONE);
                    if (myflag == true) {
                        h.img_pl_reward.setVisibility(View.VISIBLE);
//                        h.img_pl_reward.setImageResource(R.drawable.chongyanng);
//                        BbtApplication.getInstance().display(uplan.getFreshPictureAddress(), h.img_pl_reward);
                        BbtApplication.getInstance().display(list.get(groupPosition).getList().get(childPosition).getFlagRestivalAddress(), h.img_pl_reward);
                    } else {
                        h.img_pl_reward.setVisibility(View.GONE);
                    }
                }

            }

        }
//		h.img_pl_reward.setImageBitmap(BitmapUtils.readBitMap(context, R.drawable.ptjl));
        if (list.get(groupPosition).getList().get(childPosition).getFixedRateAdd().equals("0")) {
            h.li_jiaxi.setVisibility(View.GONE);
//			h.tv_rate.setText(df.format(Double.parseDouble(list.get(groupPosition).getList().get(childPosition).getFixedRate())));			
//				h.li_redjiaxi.setVisibility(View.GONE);
//				h.tv_rate.setText(df.format(Double.parseDouble(list.get(groupPosition).getList2().get(childPosition).getRate()))+"%");							
            h.tv_rate.setText(df.format(Double.parseDouble(list.get(groupPosition).getList().get(childPosition).getFixedRate())));
        } else {
            h.li_jiaxi.setVisibility(View.VISIBLE);
//				h.li_jiaxi.setBackgroundResource(R.drawable.jiaxi);
            h.tv_rate.setText(df.format(Double.parseDouble(list.get(groupPosition).getList().get(childPosition).getFixedRate()) - Double.parseDouble(list.get(groupPosition).getList().get(childPosition).getFixedRateAdd())));
            h.tv_rateadd.setText(df.format(Double.parseDouble(list.get(groupPosition).getList().get(childPosition).getFixedRateAdd())) + "%");
        }


        h.tv_time.setText(list.get(groupPosition).getList().get(childPosition).getDays());
        h.tv_investAmount.setText(Regular.getDecimalFormatTwo(Double.parseDouble(list.get(groupPosition).getList().get(childPosition).getRemainAmount()) / 10000));


        return convertView;

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

    private class Help {
        private TextView tv_title, tv_red, tv_coupon;
        private TextView tv_minamount, tv_fresh;
        private LinearLayout li_newuser;
        private TextView text_zhekou;
        private ImageView img_group;
    }

    class ViewHelp {

        private TextView v6_ud_invest_increaseNum, tv_pl_reward;
        private TextView tv_name, tv_coupon, tv_rate, tv_rateadd, tv_time, tv_investAmount, tv_newuser, tv_coupon_type, newuser;
        private LinearLayout li_jiaxi, li_redjiaxi, li_intent;
        private Button btn_invest;
        private ProgressBar sk_progresss;
        private ImageView img_list_status;
        private ImageView img_pl_reward;
        private ImageView img_group;
        private TextView text_zhekou;
    }
}

